
export function px2rem(px) {
  const ratio = 375 / 10
  return px / ratio
}

export function realPx(px) {
  const maxWidth = window.innerWidth > 500 ? 500 : window.innerWidth
  return px * (maxWidth / 375)
}

export function getSomeData(config = {
  alias: '',
  params: null,
  time: 600000
}, fn){

    if(!('alias' in config)) throw new TypeError('第一个参数没有alias 请传入');

    let { alias, params, time} = config;
    if(typeof time !== 'number'){
      time = 600000;
    }

    let localData = localStorage.getItem(alias);

    if(localData){
        localData = JSON.parse(localData);
        if(Date.now() - localData.time > time){

          return saveData();
        }else{

          return  Promise.resolve(localData.data);
        }
    }else{
      return saveData()
    }

    function saveData(){
      let theable = params === null || !params ? fn() : fn(params);

      if('then' in theable && typeof theable.then === 'function'){
        return theable.then(res => {

          localStorage.setItem(alias,JSON.stringify({
            data: res,
            time: Date.now()
          }))

          return res;
        })  
      }
    }
}

export function dformatTimetemp(timetemp){
  return new Date(timetemp).toLocaleDateString().replace(/\//g, '-')
}